apiready = function(){
seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper"], function(V, $api, $, app, service, Swiper) {
  var NVTabBar = api.require('NVTabBar');
  var frames_list;
    var vm = {
        el: '.container',
        data: {
            classfiyList: [],
            selectPage: 0,
            showTopNav: true,
            is_yzc: false,
            cat_id: '',
            cartCount: 0,
            number:0,
            bs: "" //ajax弹出服务器提示控制变量
        },
        created: function() {
            var that = this;
            // $api.clearStorage();
            if(api.pageParam.channel == 'YK'){
              console.log('通过游客登录进来的');
              frames_list = [{
                  name: 'frame1',
                  url: './shop_index_fram_ios.html',
                  bgColor: '#fff',
                  bounces:false
              },
              {
                  name: 'frame4',
                  url: '../buy_member_goods/member_goods_index.html',
                  bgColor: '#fff'
              }, {
                  name: 'frame5',
                  url: '../buy_person/person_ios.html',
                  bgColor: '#fff'
              }]
              this.init(function() {
                  that.classfiyList = json_category_list;
              })
            }else{
              // api.addEventListener({
              //     name: 'keyback'
              // }, function(ret, err){
              //     api.closeWidget();
              // });
              frames_list = [{
                  name: 'frame1',
                  url: './shop_index_fram.html',
                  bgColor: '#fff',
                  bounces:false
              },
              {
                  name: 'frame4',
                  url: '../buy_member_goods/member_goods_index.html',
                  bgColor: '#fff'
              }, {
                  name: 'frame5',
                  url: '../buy_person/person.html',
                  bgColor: '#fff',
                  useWKWebView:true
              }]
              if (!$api.getStorage("user")) {
                  app.is_login();
              } else {
                  this.bs = 1;
                  this.intoShop(function() {
                      that.init(function() {
                          // that.getIndexNav();
                          console.log(9999999);
                          that.classfiyList = json_category_list;
                      });
                      that.pushInit()
                  });
                  app.checkAppVersion(function(offon,data){
                    if(offon){//需要更新
                      setTimeout(function(){
                        api.openFrame({
                            name: 'version',
                            url: 'widget://src/Shop/shop_index/version.html',
                            rect: {
                                x: 0,
                                y: 0,
                                w: api.winWidth,
                                h: api.winHeight
                            },
                            pageParam: {
                                android_URL: data.androidurl,
                                iosURl:data.iosurl
                            },
                            bgColor: 'rgba(0,0,0,0)'
                        });
                      },2000)
                    }
                  })
              }
            }

            api.addEventListener({
                name: 'lisentCart'
            }, function(ret, err) {
                if (ret) {
                    that.getCartCount();
                }
            });
        },
        mounted: function(){
          if(api.pageParam.channel == 'YK'){
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err){
                api.closeWin()
            });
          }else{
            var lastBackKeyTime = null;
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err) {
                var now = new Date();
                var nowTime = now.getTime();
                if (lastBackKeyTime) {
                    var pastTime = nowTime - lastBackKeyTime;
                    if (pastTime < 3000) {
                        api.closeWidget({
                            id: api.appId,
                            retData: {
                                name: 'closeWidget'
                            },
                            silent: true
                        });
                        return;
                    }
                }
                api.toast({
                    msg: '再按一次返回键退出！',
                    duration: 2000,
                    location: 'bottom'
                });
                lastBackKeyTime = nowTime;
            });
          }
        },
        methods: {

            init: function(callback) {
              var that = this;
                api.openFrameGroup({
                    name: 'shop_index',
                    background: '#fff',
                    scrollEnabled: false,
                    preload: 2,
                    rect: {
                        x: 0,
                        y: 0,
                        w: 'auto',
                        h: 'auto',
                        marginBottom: 0
                    },
                    index: 1,
                    frames: frames_list
                }, function(ret, err) {
                    if (ret) {
                      NVTabBar.bringToFront();
                        callback();
                    } else {
                        alert(JSON.stringify(err));
                    }
                });

                this.switchPage(1, function(){
                  that.initNav(1);
                  console.log('执行购物车商品统计');
                  that.getCartCount();
                  return
                });

                setTimeout(function(){
                  if($api.getStorage('user')){
                    if(!$api.getStorage('user').data.higher.out_user_id){//没有上级
                      that.openDialog()
                    }
                  }
                },1000)
            },
            // 极光推送初始化
            pushInit:function(){
              var that =this,ajpush = api.require('ajpush');
              if (api.systemType == 'ios') {
                  ajpush.bindAliasAndTags({
                      alias: $api.getStorage('user').data.out_user_id,
                      tags: []
                  }, function(ret) {});
                  api.addEventListener({
                      name: 'noticeclicked'
                  }, function(ret, err) {
                      that.setPage_type(ret.value.extras.message_type)
                  });
              } else {
                  ajpush.init(function(ret) {
                      ajpush.bindAliasAndTags({
                          alias: $api.getStorage('user').data.out_user_id,
                          tags: []
                      }, function(ret) {

                      });
                  });
              }
              // 监听安卓点击
              api.addEventListener({
                  name: 'appintent'
              }, function(ret, err) {
                  if (ret && ret.appParam.ajpush) {
                      if (ret.appParam.ajpush.extra) {
                          that.setPage_type(ret.appParam.ajpush.extra.message_type)
                      }
                  }
              })
            },
            //点击通知后监听消息类型
            setPage_type:function(message_type){
              if (message_type == "returns_account") { //收益到账
                  api.openWin({
                      name: 'redHblog',
                      url: 'widget://pages/map/new_redHblog.html'
                  });
              }
            },
            // 打开邀请码弹窗
            openDialog:function(){
              api.openFrame({
                  name: 'dialog_fm',
                  url: './dialog_fm.html',
                  rect:{
                      x: 0,
                      y: 0,
                      w: api.winWidth,
                      h: api.winHeight
                  },
                  allowEdit:true,
                  bounces: false,
                  bgColor: 'rgba(0,0,0,.4)'
              });
            },
            initNav: function(inds) {
                var that = this;
                NVTabBar.close();
                NVTabBar.open({
                    styles: {
                        bg: '#fff',
                        h: 50,
                        dividingLine: {
                            width: 1,
                            color: '#efefef'
                        },
                        badge: {
                            bgColor: '#FF3030',
                            numColor: '#fff'
                        }
                    },
                    items: [{
                        w: api.winWidth / 3.0,
                        bg: {
                            marginB: -2,
                            image: '#fff'
                        },
                        iconRect: {
                            w: 25.0,
                            h: 25.0,
                        },
                        icon: {
                            normal: 'widget://image/footer/home1.png',
                            highlight: 'widget://image/footer/home2.png',
                            selected: 'widget://image/footer/home2.png'
                        },
                        title: {
                            text: '首页',
                            size: 11.0,
                            normal: '#666666',
                            selected: '#000',
                            marginB: 6.0
                        }
                    },
                    {
                      w: api.winWidth / 3.0,
                      bg: {
                        marginB: 8,
                        image: 'rgba(0,0,0,0)' //中间背景图
                      },
                      iconRect: {
                        w: 60,
                        h: 60,
                      },
                      icon: {
                          normal: 'widget://image/footer/center.png',
                          highlight: 'widget://image/footer/center.png',
                          selected: 'widget://image/footer/center.png'
                      },
                      title: {
                          text: '',
                          size: 11.0,
                          normal: '#666666',
                          selected: '#000',
                          marginB: 6.0
                      }
                    }, {
                        w: api.winWidth / 3.0,
                        bg: {
                            marginB: -2,
                            image: '#fff'
                        },
                        iconRect: {
                            w: 25.0,
                            h: 25.0,
                        },
                        icon: {
                            normal: 'widget://image/footer/user1.png',
                            highlight: 'widget://image/footer/user2.png',
                            selected: 'widget://image/footer/user2.png'
                        },
                        title: {
                            text: '个人中心',
                            size: 11.0,
                            normal: '#666666',
                            selected: '#000',
                            marginB: 6.0
                        }
                    }],
                    selectedIndex: inds
                }, function(ret, err) {
                  console.log(192939239);
                  console.log(JSON.stringify(ret));
                    NVTabBar.bringToFront();
                    if (ret.index == undefined) {
                        ret.index = inds
                    }
                    if (ret.index != 0) {
                      api.setFrameGroupAttr({
                          name: 'home_group',
                          hidden: true
                      });
                    }else{
                      api.sendEvent({
                          name: 'getMessageNotReadCount'
                      });

                      api.setFrameGroupAttr({
                          name: 'home_group',
                          hidden: false
                      });
                    }
                    if(!ret.index==1){
                      api.setFrameAttr({
                          name: 'upgrade_fm',
                          hidden:true
                      });
                      // api.closeFrame({
                      //     name: 'dialog_fm'
                      // });
                    }
                    if(ret.index!=2){
                      // api.closeFrame({
                      //     name: 'dialog_fm'
                      // });
                    }
                    if (ret.index == 2 || ret.index == 1) {
                      //执行person.js intoShop方法
                      //执行buyMemberGoods.js init方法
                        api.sendEvent({
                            name: 'login_flag'
                        });
                    }
                    that.switchPage(ret.index, function(){});
                    console.log('initNav方法结束');
                });
            },

            // 进入商城
            intoShop: function(cb) {
                var that = this;
                app.ajax(
                    service['getUserShopInfo'], {
                        data: {
                            values: {
                                appid: app.appid,
                                userid: $api.getStorage("user").data.id,
                                mask: 'none',
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                      console.log("执行intoShop获取shop_unionid");
                      console.log(JSON.stringify(res));
                        if (res.code == 0) {
                            $api.setStorage("shop_unionid", res.data.shop_unionid);
                            $api.setStorage("shop_data", res.data);
                            $api.setStorage("user", res);
                            cb();
                        }
                    })
            },

            // 获取购物车数量
            getCartCount: function() {
              return
                var that = this;
                app.ajax(
                    service['cartGetCartCount'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.cartCount = res.data;
                            // if (that.cartCount > 0) {
                            //     NVTabBar.setBadge({
                            //         // index: 3,
                            //         index: 1,
                            //         badge: ''
                            //     });
                            // } else {
                            //     NVTabBar.setBadge({
                            //         // index: 3
                            //         index: 1
                            //     });
                            // }
                        } else {
                            // api.toast({
                            //     msg: res.message,
                            //     duration: 2000,
                            //     location: 'bottom'
                            // });
                        }
                    }
                );
            },

            // 选择分类
            selectNav: function(index, id) {
                var domArr = document.querySelectorAll('.top-nav li');
                for (var i = 0, len = domArr.length; i < len; i++) {
                    domArr[i].classList.remove('select-nav');
                }
                if (index != -1) {
                    domArr[index + 1].classList.add('select-nav');
                } else {
                    domArr[0].classList.add('select-nav');
                }
                var cat_id;
                id ? cat_id = id : cat_id = 0;
                this.cat_id = id ? cat_id = id : cat_id = 0;
                api.sendEvent({
                    name: 'switchIndexFrame',
                    extra: {
                        cat_id: cat_id
                    }
                });
            },
            // 获取分类
            getIndexNav: function() {
                var that = this;
                app.ajax(
                    service['homeGetSysCategory'], {
                        data: {
                            values: {
                                cat_id: 0,
                                mask: 'none'
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.classfiyList = res.data;
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },
            // 切换分页
            switchPage: function(idx, cb) {
                this.selectPage = idx;
                if (idx == 0) {
                    this.showTopNav = true;
                    api.setFrameGroupAttr({
                        name: 'shop_index',
                        rect: {
                            marginBottom: 0,
                        }
                    });
                    // api.sendEvent({
                    //   name: 'switchIndexFrame',
                    //     extra: {
                    //         cat_id: this.cat_id
                    //     }
                    // });
                }else if(idx == 1 || idx == 2){
                  this.showTopNav = false;
                  api.setFrameGroupAttr({
                      name: 'shop_index',
                      rect: {
                          marginBottom: 50,
                      }
                  });
                } else {
                    this.showTopNav = false;
                    api.setFrameGroupAttr({
                        name: 'shop_index',
                        rect: {
                            marginBottom: 0,
                        }
                    });
                }

                api.setFrameGroupIndex({
                    name: 'shop_index',
                    index: idx
                });

                cb();
            }
        }
    };
    var even = function() {
      api.addEventListener({
          name: 'openDialog'
      }, function(ret, err){
          newVue.openDialog()
      });

      api.addEventListener({
          name: 'toStar'
      }, function(ret, err){
        newVue.switchPage(1,function(){})
        api.setFrameGroupAttr({
            name: 'home_group',
            hidden: true
        });
        NVTabBar.setSelect({
            index: 1,
            selected: true
        });
      });

        api.addEventListener({
            name: 'Openshop_fm'
        }, function(ret, err){
          api.setFrameGroupIndex({
              name: 'shop_index',
              index: 1
          });
          NVTabBar.setSelect({
              index: 1,
              selected: true
          });
        });

        api.setStatusBarStyle({
            style: 'dark',
            color: '#DASFJHAJKS112222DFHKJASHFKJSAHDFJK'
        })

        app.BACK();
    }
    var newVue = new Vue(vm);
    even();
    // V.apiready(vm, even);
});
}
